package com.zukxu.items.project.io;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * Description: 文件读取
 *
 * @author zukxu
 * @date 2020/10/30 0030 09:25
 */
public class FileReadUtils {
	/**
	 * 字节输入流
	 *
	 * @param path
	 */
	public void readByFileInputStream(String path) {
		try (FileInputStream fis = new FileInputStream(path)) {
			byte[] bytes = new byte[1024];
			while (fis.read(bytes) != -1) {
				String str = new String(bytes);
				System.out.println(str);
			}
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 字节缓冲输入流
	 *
	 * @param path
	 */
	public void readByBufferedInputStream(String path) {
		try (BufferedInputStream fis = new BufferedInputStream(new FileInputStream(path))) {
			byte[] bytes = new byte[1024];
			while (fis.read(bytes) != -1) {
				String str = new String(bytes);
				System.out.println(str);
			}
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 字符输入流
	 *
	 * @param path
	 */
	public void readByFileReader(String path) {
		try (FileReader reader = new FileReader(path)) {
			char[] chars = new char[1024];
			while (reader.read(chars) != -1) {
				System.out.println(chars);
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 字符缓冲输入流
	 *
	 * @param path
	 */
	public void readByBufferedReader(String path) {
		try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
			String str;
			while ((str = reader.readLine()) != null) {
				System.out.println(str);
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
